/****************************************************
 * Description: Controllar for 角色权限
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
	*  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuol.bfsuolframework.security.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bfsuol.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuol.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuol.bfsuolframework.core.controller.support.Pagination;
import com.bfsuol.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuol.bfsuolframework.core.spring.QueryParameter;
import com.bfsuol.bfsuolframework.security.dao.RolePrivilege;
import com.bfsuol.bfsuolframework.security.service._RolePrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/components/security/rolePrivilege")
public class _RolePrivilegeController extends SpringControllerSupport {

    @Autowired
    private _RolePrivilegeService rolePrivilegeService;
    
	@RequestMapping("/index")
	public ModelAndView index(Model model){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("index"));
		return modelAndView;
	}
	
	@RequestMapping("/list")
	public ModelAndView list(
			@QueryParameter("query") QueryParam query,
			@ModelAttribute("page") Pagination page){

		com.baomidou.mybatisplus.extension.plugins.pagination.Page page1 = new Page(page.getDefaultCurrentPage(),page.getPageSize());
		com.baomidou.mybatisplus.extension.plugins.pagination.Page privilegePages = rolePrivilegeService.page(page1,new QueryWrapper<>());
		page.setItems(privilegePages.getRecords());
		page.setTotalRecord((int) privilegePages.getTotal());

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("list"));
		return modelAndView;
	}
	
	@RequestMapping("/input")
	public ModelAndView create(@ModelAttribute RolePrivilege rolePrivilege,Model model){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("input"));
		return modelAndView;
	}
	
	@RequestMapping("/input/{id}")
	public ModelAndView edit(@PathVariable("id") Long id, Model model){
		RolePrivilege rolePrivilege = rolePrivilegeService.getById(id);
		model.addAttribute(rolePrivilege);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(getViewNamedPath("input"));
		return modelAndView;
	}
	
	@RequestMapping("/save")
	public @ResponseBody MessageJson save(@ModelAttribute RolePrivilege rolePrivilege){
		if(rolePrivilege.isNew()){
			rolePrivilegeService.save(rolePrivilege);
		}else{
			rolePrivilegeService.updateById(rolePrivilege);
		}
		return MessageJson.information("保存成功");
	}
	
	
	@RequestMapping("/delete/{id}")
	public @ResponseBody MessageJson delete(@PathVariable("id") Long id){
		rolePrivilegeService.removeById(id);
		return MessageJson.information("成功删除1条");
	}
	
	@RequestMapping("/delete")
	public @ResponseBody MessageJson delete(@RequestParam("ids") Long[] ids){
		if(ids == null || ids.length == 0){
			return MessageJson.error("没有删除");
		}
		for(Long id : ids){
			rolePrivilegeService.removeById(id);
		}
		return MessageJson.information("成功删除"+ids.length+"条");
	}
}
